package com.enqualcomm.kids.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.enqualcomm.kids.R;

import common.utils.DensityUtil;

/**
 * Created by Administrator on 2016/6/12.
 */
public class SlidingView extends RelativeLayout {
    private TextView slide_bg_tv;
    private ImageView silde_img_btn;

    public SlidingView(Context context) {
        super(context);
        init(context);
    }

    public SlidingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SlidingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.slide_view, this);
        slide_bg_tv = (TextView) findViewById(R.id.slide_bg_tv);
        silde_img_btn = (ImageView) findViewById(R.id.silde_img_btn);
    }

    private boolean touchInButton;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (!isSlideViewEnable) {
            return super.dispatchTouchEvent(ev);
        }
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Rect rect = new Rect();
                silde_img_btn.getGlobalVisibleRect(rect);
                touchInButton = rect.contains((int) ev.getRawX(), (int) ev.getRawY());
                break;
            case MotionEvent.ACTION_MOVE:
                if (!touchInButton) {
                    break;
                }
                moveEventAction(ev);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (!touchInButton) {
                    break;
                }
                upEvevtAction(ev);
                touchInButton = false;
                break;

        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    private int maxMove = 0;

    private void moveEventAction(MotionEvent event) {
        float transX = event.getX() - silde_img_btn.getWidth() / 2;
        transX = transX < 0 ? 0 : transX;
        int max = getWidth() - silde_img_btn.getWidth() - DensityUtil.dip2px(getContext(), 2);
        maxMove = max;
        transX = transX > max ? max : transX;
        silde_img_btn.setTranslationX(transX);
    }

    private void upEvevtAction(MotionEvent event) {
        float transX = event.getX() - silde_img_btn.getWidth() / 2;
        if (transX < (maxMove * (2 / 3.0))) {
            silde_img_btn.setTranslationX(getX() - getLeft() + DensityUtil.dip2px(getContext(), 2));
            isAtEnd = false;
        } else {
            silde_img_btn.setTranslationX(maxMove);
            slide_bg_tv.setText(getResources().getString(R.string.slide_after));
            slide_bg_tv.setBackgroundColor(Color.rgb(114, 198, 38));
            slide_bg_tv.setTextColor(Color.rgb(255, 255, 255));
            isSlideViewEnable = false;
            isAtEnd= true;
            listener.scrollEndSuccess();
        }
    }


    private OnScrollEndListener listener;

    public void setOnScrollEndListener(OnScrollEndListener listener) {
        this.listener = listener;
    }

    public interface OnScrollEndListener {
        void scrollEndSuccess();
        void resetView();
    }

    private boolean isSlideViewEnable = false;

    public void setSlideViewEnable(boolean enable) {
        isSlideViewEnable = enable;
    }

    public void resetSlidView() {
        isAtEnd= false;
        silde_img_btn.setTranslationX(getX() - getLeft() + DensityUtil.dip2px(getContext(), 2));
        slide_bg_tv.setBackgroundColor(Color.rgb(191, 145, 88));
        slide_bg_tv.setText(getResources().getString(R.string.slide_before));
        slide_bg_tv.setTextColor(Color.rgb(229, 255, 206));
        listener.resetView();
    }

    private boolean isAtEnd = false;
    public boolean isAtEnd(){
        return isAtEnd;
    }
}
